package com.ag.lzm.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.ag.bean.TbPlan;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.lzm.service.IPlanService;
import com.ag.lzm.utils.FileUtil;
import com.opensymphony.xwork2.ActionContext;

/**
 * 预案主要操作类
 * 
 * @author Administrator
 * 
 */
public class PlanAction extends BaseAction implements SessionAware,
		RequestAware {
	private static final long serialVersionUID = 2504301690626755445L;
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "LZMFileUtil")
	private FileUtil fileUtil;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;

	private Map<String, Object> requestMap;
	/**
	 * 查询所有记录数的返回载体
	 */
	private Long recordCount;
	private Map<String, String> queryOption;
	private String planStatus;
	private String PId;
	private String ajaxRst;
	private List<TbPlan> planList;
	private IPlanService planService;
	private TbPlan tbPlan;
	private int firstRow;
	private int maxRow;

	private String basePath;

	public String getBasePath() {
		return basePath;
	}

	/**
	 * 发布预案
	 * 
	 * @return
	 */
	public String releasePlan() {
		try {
			planService.releasePlan(PId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 根据预案ID报备预案
	 * 
	 * @return
	 */

	public String reportPreparedPlan() {
		try {
			planService.reportPreparedPlan(PId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 根据ID删除未报备的预案
	 * 
	 * @return
	 */

	public String delPlanById() {
		try {
			planService.delPlanById(PId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 根据ID查找未报备的预案
	 * 
	 * @return
	 */

	public String findPlanById() {
		try {
			ActionContext ac = ActionContext.getContext();
			ServletContext sc = (ServletContext) ac
					.get(ServletActionContext.SERVLET_CONTEXT);
			HttpServletRequest request = ServletActionContext.getRequest();
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
			tbPlan = planService.findPlanById(PId, sc.getRealPath("/"),
					basePath);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_plan_no_report_by_id";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 为颢铧跳出提供Action
	 * 
	 * @return
	 */

	public String findPlan() {
		try {
			ActionContext ac = ActionContext.getContext();
			ServletContext sc = (ServletContext) ac
					.get(ServletActionContext.SERVLET_CONTEXT);
			HttpServletRequest request = ServletActionContext.getRequest();
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
			tbPlan = planService.findPlanById(PId, sc.getRealPath("/"),
					basePath);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_plan_no_report_by_id";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找所有预案记录数
	 * 
	 * @return
	 */
	public String findAllPlanRC() {
		try {
			recordCount = planService.findAllPlanRC(queryOption, planStatus,
					sessionMap);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_record_count";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找所有报备预案记录
	 * 
	 * @return
	 */

	public String findAllPlan() {
		try {
			planList = planService.findAllPlan(queryOption, planStatus,
					firstRow, maxRow, sessionMap);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_all_plan";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	public void setPlanService(IPlanService planService) {
		this.planService = planService;
	}

	public void setFirstRow(int firstRow) {
		this.firstRow = firstRow;
	}

	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public void setQueryOption(Map<String, String> queryOption) {
		this.queryOption = queryOption;
	}

	public String getAjaxRst() {
		return ajaxRst;
	}

	public List<TbPlan> getPlanList() {
		return planList;
	}

	public Long getRecordCount() {
		return recordCount;
	}

	public Map<String, String> getQueryOption() {
		return queryOption;
	}

	public TbPlan getTbPlan() {
		return tbPlan;
	}

	public void setPId(String id) {
		PId = id;
	}

	public void setPlanStatus(String planStatus) {
		this.planStatus = planStatus;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		requestMap = request;
	}
}
